package com.qf.redisdemo01.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author q
 * @version V1.0
 * @date 2021/12/16
 **/
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory){
        // 创建一个RedisTemplate对象， 指定key使用的是string序列化器， value不指定则使用默认jdk序列化器
        RedisTemplate<String, Object> stringObjectRedisTemplate = new RedisTemplate<>();
        stringObjectRedisTemplate.setKeySerializer(new StringRedisSerializer());
        stringObjectRedisTemplate.setConnectionFactory(factory);
        return stringObjectRedisTemplate;
    }

    @Bean
    public Config config(){
        Config config = new Config();
        //config.useClusterServers().addNodeAddress("redis://192.168.10.137:7001","redis://192.168.10.137:7002","redis://192.168.10.137:7003");
        //config.useClusterServers().addNodeAddress("redis://192.168.10.137:7001");
        config.useSingleServer().setAddress("redis://192.168.10.137:6379");
        config.useSingleServer().setPassword("123456");
        return config;
    }

    @Bean
    public RedissonClient redissonClient(Config config){
        return Redisson.create(config);
    }

}
